package cn.stylefeng.guns.modular.business.frequency.finance.mapper;

import cn.stylefeng.guns.modular.business.frequency.finance.model.param.FinanceDTO;
import cn.stylefeng.guns.modular.business.frequency.finance.model.result.*;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.model.param.PkgFinanceGross;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import cn.stylefeng.guns.modular.business.frequency.finance.entity.Finance;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单财务信息表 Mapper 接口
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
public interface FinanceMapper extends BaseMapper<Finance> {

    void updateCollectionSettlementInfo(@Param("orderId") Long orderId, @Param("settlementName") String settlementName,
                                        @Param("settlementPhone") String settlementPhone);

  
    void updateKickBackFeeByOrderId(@Param("orderId") String orderId,
                                    @Param("unSettlementFee") BigDecimal unSettlementFee,
                                    @Param("hasSettlementFee") BigDecimal hasSettlementFee,
                                    @Param("settlementUserName") String settlementUserName,
                                    @Param("settlementUserPhone") String settlementUserPhone);


    @Update("update tt_finance set spot_has = #{hasSettlementFee}, spot_un = #{unSettlementFee}, " +
            "spot_settlement_name = #{settlementUserName}, spot_settlement_phone = #{settlementUserPhone} " +
            "where order_id = #{orderId}")
    void updateSpotPayFeeByOrderId(@Param("orderId") String orderId,
                                   @Param("unSettlementFee") BigDecimal unSettlementFee,
                                   @Param("hasSettlementFee") BigDecimal hasSettlementFee,
                                   @Param("settlementUserName") String settlementUserName,
                                   @Param("settlementUserPhone") String settlementUserPhone);

    @Update("update tt_finance set delivery_has = #{hasSettlementFee}, delivery_un = #{unSettlementFee}, " +
            "delivery_settlement_name = #{settlementUserName}, delivery_settlement_phone = #{settlementUserPhone} " +
            "where order_id = #{orderId}")
    void updateDeliveryFeeByOrderId(@Param("orderId") String orderId,
                                    @Param("unSettlementFee") BigDecimal unSettlementFee,
                                    @Param("hasSettlementFee") BigDecimal hasSettlementFee,
                                    @Param("settlementUserName") String settlementUserName,
                                    @Param("settlementUserPhone") String settlementUserPhone);

    @Update("update tt_finance set delivery_has = #{hasSettlementFee}, delivery_un = #{unSettlementFee} " +
            "where order_id = #{orderId}")
    void updateDeliveryFeeOnly(@Param("orderId") String orderId,
                               @Param("unSettlementFee") BigDecimal unSettlementFee,
                               @Param("hasSettlementFee") BigDecimal hasSettlementFee);

    @Update("update tt_finance set back_has = #{hasSettlementFee}, back_un = #{unSettlementFee}, " +
            "back_settlement_name = #{settlementUserName}, back_settlement_phone = #{settlementUserPhone} " +
            "where order_id = #{orderId}")
    void updateBackPayFeeByOrderId(@Param("orderId") String orderId,
                                   @Param("unSettlementFee") BigDecimal unSettlementFee,
                                   @Param("hasSettlementFee") BigDecimal hasSettlementFee,
                                   @Param("settlementUserName") String settlementUserName,
                                   @Param("settlementUserPhone") String settlementUserPhone);

    @Update("update tt_finance set slot_has = #{hasSettlementFee}, slot_un = #{unSettlementFee}, " +
            "slot_settlement_name = #{settlementUserName}, slot_settlement_phone = #{settlementUserPhone} " +
            "where order_id = #{orderId}")
    void updateSlotPayFeeByOrderId(@Param("orderId") String orderId,
                                   @Param("unSettlementFee") BigDecimal unSettlementFee,
                                   @Param("hasSettlementFee") BigDecimal hasSettlementFee,
                                   @Param("settlementUserName") String settlementUserName,
                                   @Param("settlementUserPhone") String settlementUserPhone);

    @Update("update tt_finance set pick = #{needSettlementFee}, pick_un = #{unSettlementFee},  pick_has = #{hasSettlementFee}," +
            "pick_settlement_name = #{settlementUserName}, pick_settlement_phone = #{settlementUserPhone} " +
            "where order_id = #{orderId}")
    void updatePickFeeByOrderId(@Param("orderId") String orderId,
                                @Param("needSettlementFee") BigDecimal needSettlementFee,
                                @Param("unSettlementFee") BigDecimal unSettlementFee,
                                @Param("hasSettlementFee") BigDecimal hasSettlementFee,
                                @Param("settlementUserName") String settlementUserName,
                                @Param("settlementUserPhone") String settlementUserPhone);

    @Update("update tt_finance set pick_settlement_name = #{settlementUserName}, pick_settlement_phone = #{settlementUserPhone} " +
            "where order_id = #{orderId}")
    void updatePickSettlementInfoByOrderId(@Param("orderId") String orderId,
                                           @Param("settlementUserName") String settlementUserName,
                                           @Param("settlementUserPhone") String settlementUserPhone);

    @Update("update tt_finance set take_settlement_name = #{settlementUserName}, take_settlement_phone = #{settlementUserPhone} " +
            "where order_id = #{orderId}")
    void updateTakeSettlementInfoByOrderId(@Param("orderId") Long orderId,
                                           @Param("settlementUserName") String settlementUserName,
                                           @Param("settlementUserPhone") String settlementUserPhone);

    @Update("update tt_finance set take = #{needSettlementFee}, take_un = #{unSettlementFee},  take_has = #{hasSettlementFee}," +
            "take_settlement_name = #{settlementUserName}, take_settlement_phone = #{settlementUserPhone} " +
            "where order_id = #{orderId}")
    void updateTakeFeeByOrderId(@Param("orderId") String orderId,
                                @Param("needSettlementFee") BigDecimal needSettlementFee,
                                @Param("unSettlementFee") BigDecimal unSettlementFee,
                                @Param("hasSettlementFee") BigDecimal hasSettlementFee,
                                @Param("settlementUserName") String settlementUserName,
                                @Param("settlementUserPhone") String settlementUserPhone);

    PkgFinance getPkgFinanceByOrderId(@Param("orderId") Long orderId);

    void updateCollectionStatus(@Param("status") Integer status, @Param("orderId") Long orderId);

    void updatePaymentStatus(@Param("status") Integer status,@Param("orderId") Long orderId);


    void updateDeliveryInfo(@Param("orderId") Long orderId, @Param("deliverySettlementName") String deliverySettlementName,
                            @Param("deliverySettlementPhone") String deliverySettlementPhone);

    void clearDeliveryCollectionInfo(@Param("orderId") Long orderId);

    @Update("update tt_finance set has_transfer_voucher = 1 where order_id = #{orderId}")
    void updateHasTransferVoucher(@Param("orderId") Long orderId);

    Page<PkgFinanceCollection> getCollectionList(@Param("page") Page<Object> page, @Param("financeDTO") FinanceDTO financeDTO,@Param("tenantId")Long tenantId);
    Page<SiteFinanceCollection> getSiteCollectionList(@Param("page") Page<Object> page,@Param("tenantId")Long tenantId, @Param("user_id") Long userId);
    Page<SiteFinanceCollection> getSiteCollectionList1(@Param("page") Page<Object> page,@Param("tenantId")Long tenantId, @Param("userId") Long userId);

    PkgFinanceCollection getCollectionSummary(@Param("financeDTO") FinanceDTO financeDTO,@Param("tenantId")Long tenantId);

    Page<PkgFinancePayment> getPaymentList(@Param("page") Page<Object> page, @Param("financeDTO") FinanceDTO financeDTO,@Param("tenantId")Long tenantId);

    PkgFinancePayment getPaymentSummary(@Param("financeDTO") FinanceDTO financeDTO,@Param("tenantId")Long tenantId);

    @Update("update tt_finance set delivery_settlement_name = #{deliverySettlementName}, " +
            "delivery_settlement_phone = #{deliverySettlementPhone} where order_id = #{orderId}")
    void updateDeliveryInfo(@Param("orderId") String orderId, @Param("deliverySettlementName") String deliverySettlementName,
                            @Param("deliverySettlementPhone") String deliverySettlementPhone);

    List<PkgFinanceCheck> getFinanceCheckList(@Param("financeDTO") FinanceDTO financeDTO,@Param("tenantId")Long tenantId);

    Page<PkgFinanceGross> getFinanceGrossList(@Param("page") Page<Object> page, @Param("financeDTO") FinanceDTO financeDTO,@Param("tenantId")Long tenantId);

    PkgFinanceGross getFinanceGrossByOrderId(@Param("orderId") Long orderId);

    PkgFinance getPkgFinanceByOrderId(@Param("orderId") String orderId);

    @Update("update tt_finance set collection_status = #{status} where order_id = #{orderId}")
    void updateCollectionStatus(@Param("status") Integer status, @Param("orderId") String orderId);

    @Update("update tt_finance set payment_status = #{status} where order_id = #{orderId}")
    void updatePaymentStatus(@Param("status") Integer status, @Param("orderId") String orderId);

    @Update("update tt_finance set has_online_pay = 1 where order_id = #{orderId}")
    void updateHasOnlinePay(@Param("orderId") String orderId);

    @Update("update tt_finance set pick = null,pick_has = null,pick_un = null,pick_settlement_name = '', pick_settlement_phone = '', pick_settlement_type = 0 where order_id = #{orderId}")
    void updatePickCarFeeNull(@Param("orderId") String orderId);

    @Update("update tt_finance set take = null,take_has = null,take_un = null,take_settlement_name = '', take_settlement_phone = '', take_settlement_type = 0 where order_id = #{orderId}")
    void updateTakeCarFeeNull(@Param("orderId") String orderId);

    void updateBaseCollectionFeeNull(@Param("orderId") String orderId);

    @Select("select collection_status from tt_finance where order_id = #{orderId}")
    int getCollectionStatus(@Param("orderId") String orderId);

    @Update("update tt_finance set delivery_has_collection = 1 where order_id = #{orderId}")
    void updateDeliveryHasCollection(@Param("orderId") String orderId);

    void clearCollectionInfo(@Param("orderId") String orderId);



    @Update("update tt_finance set pick = null,pick_un=null,pick_has=null,pick_settlement_name='',pick_settlement_phone=''" +
            ",pick_settlement_type=0 where order_id = #{orderId}")
    void clearPickInfo(@Param("orderId") String orderId);

    @Update("update tt_finance set take = null,take_un=null,take_has=null,take_settlement_name='',take_settlement_phone=''" +
            ",take_settlement_type=0 where order_id = #{orderId}")
    void clearTakeInfo(@Param("orderId") String orderId);


    void updateCollectionSettlementInfo(@Param("orderId") String orderId, @Param("settlementName") String settlementName,
                                        @Param("settlementPhone") String settlementPhone);

    PkgFinanceCheck getFinanceCheckSummary(@Param("financeDTO") FinanceDTO financeDTO,@Param("tenantId")Long tenantId);

    PkgFinanceGross getFinanceGrossSummary(@Param("financeDTO") FinanceDTO financeDTO,@Param("tenantId")Long tenantId);


    Page<SiteFinanceCollection> sitePayment(@Param("page") Page<Object> defaultPage, @Param("uid") Long sysLoginUserId, @Param("tenantId")Long tenantId);

    List<FinanceSettlementChannelResult> getFinanceSettlementChannel(@Param("tenantId")Long tenantId);
}